package com.wnhz.work.jy.activity;

import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.MaxMinNumber;
import com.wnhz.work.jy.bean.TrendChartBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.DensityUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;
import com.wnhz.work.jy.view.ScoreTrend;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TrendChartActivity extends BaseActivity implements View.OnClickListener, ScoreTrend.ScoreTrendClick {
    private TextView title;
    private List<TextView> texts = new ArrayList<>();
    private TextView day_text, week_text, month_text;
    private int type_index = 2;
    private int text_size;

    private TextView tv_time;
    private String goodsId;

    private TextView type_name;
    private TextView change_number;

    private TrendChartBean bean;
    private String[] monthText;
    private List<Double> score = new ArrayList<>();
    private List<String> times = new ArrayList<>();
    private ScoreTrend scoreTrend;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    type_name.setText(bean.getTitle());
                    change_number.setText(bean.getNowPrice());
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trend_chart);
        goodsId = getIntent().getStringExtra("goodsId");
        initTirle();
        scoreTrend = (ScoreTrend) findViewById(R.id.scoreTrend);
        day_text = (TextView) findViewById(R.id.day_text);
        week_text = (TextView) findViewById(R.id.week_text);
        month_text = (TextView) findViewById(R.id.month_text);
        tv_time = (TextView) findViewById(R.id.tv_time);
        type_name = (TextView) findViewById(R.id.type_name);
        change_number = (TextView) findViewById(R.id.change_number);
        texts.add(day_text);
        texts.add(week_text);
        texts.add(month_text);

        initClick();


        setType(type_index);

//        showDialog();
        uploadByxUtils();


    }

    private void setScoreTrendData(String[] monthText, List<Double> score) {

        MaxMinNumber number = getMaxMinNumber(score);
        scoreTrend.setMonthText(monthText);
        scoreTrend.setScore(score);
        scoreTrend.setTrendClick(this);
        scoreTrend.setMaxScore((int) number.getMaxNumbrt());
        scoreTrend.setMinScore((int) number.getMinNumber());
        scoreTrend.refult();
    }

    private static final String TAG = "TrendChartActivity";

    private void uploadByxUtils() {
        showProgress("加载中...");
        Map<String, Object> map = new HashMap<>();
//        map.put("goodsId", goodsId);
        map.put("goodsId", goodsId);
        map.put("type", "3");
        XUtil.Post(ConfigUtil.MOVEMENTSDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                System.out.println("=======goodsId"+result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    bean = new TrendChartBean();
                    bean = gson.fromJson(jsonObject.optString("charts"), TrendChartBean.class);
                    Log.e(TAG, "onSuccess: " + bean.toString());
                    int mon_size = bean.getAbscissa().size();
                    monthText = new String[mon_size];
                    List<String> text = new ArrayList<String>();
                    for (int i = mon_size - 1; i >= 0; i--) {
                        text.add(bean.getAbscissa().get(i));

                    }
                    for (int i = 0; i < text.size(); i++) {
                        monthText[i] = text.get(i);
                    }
                    score.clear();
                    for (int i = bean.getPrice().size() - 1; i >= 0; i--) {
                        double d = Double.parseDouble(bean.getPrice().get(i));
                        score.add(d);
                    }
                    times.clear();
                    for (int i = bean.getWhatday().size() - 1; i >= 0; i--) {
                        times.add(bean.getWhatday().get(i));
                    }
                    change_number.setText(bean.getNowPrice());
                    type_name.setText(bean.getTitle());
                    if (times.size() > 0)
                        tv_time.setText(times.get(0));
                    setScoreTrendData(monthText, score);
                    /*JSONObject jsonObject1 = jsonObject.optJSONObject("charts");
                    String title = jsonObject1.optString("title");
                    List<String> whatday1 = new ArrayList<>();
                    List<String> price = new ArrayList<>();
                    List<String> abscissa = new ArrayList<>();
                    JSONArray jsonArray1 = jsonObject1.optJSONArray("whatday");
                    for (int i = 0; i < jsonArray1.length(); i++) {
                        String s = jsonArray1.optString(i);
                        whatday1.add(s);
                    }
                    JSONArray jsonArray2 = jsonObject1.optJSONArray("price");
                    for (int i = 0; i < jsonArray2.length(); i++) {
                        String s = jsonArray1.optString(i);
                        price.add(s);
                    }
                    JSONArray jsonArray3 = jsonObject1.optJSONArray("abscissa");
                    for (int i = 0; i < jsonArray3.length(); i++) {
                        String s = jsonArray1.optString(i);
                        abscissa.add(s);
                    }
                    String nowPrice = jsonObject1.optString("nowPrice");

                    bean = new TrendChartBean();
                    bean.setTitle(title);
                    bean.setWhatday(whatday1);
                    bean.setAbscissa(abscissa);
                    bean.setPrice(price);
                    bean.setNowPrice(nowPrice);

                    System.out.println(bean.toString() + "aaaaaaeee");
                    mHandler.sendEmptyMessage(1);*/

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

        });

    }


    private void initClick() {
        text_size = texts.size();
        for (int i = 0; i < text_size; i++) {
            texts.get(i).setOnClickListener(this);
        }
    }

    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("金币走势");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.day_text:
                type_index = 0;

                setType(type_index);
                break;
            case R.id.week_text:
                type_index = 1;
                setType(type_index);
                break;
            case R.id.month_text:
                type_index = 2;
                setType(type_index);
                break;
        }
    }

    private void setType(int index) {
        for (int i = 0; i < text_size; i++) {
            texts.get(i).setTextColor(0xff666666);
            if (index == i) {
                texts.get(i).setTextColor(0xff000000);
                texts.get(i).setSelected(true);
            } else
                texts.get(i).setSelected(false);

            texts.get(i).setBackgroundDrawable(setShape(i, texts.get(i).isSelected()));
        }
    }

    private GradientDrawable setShape(int index, boolean select) {
        GradientDrawable drawable = new GradientDrawable();
        float b = DensityUtils.dp2px(this, 3);

        float[] radius = new float[]{0, 0, 0, 0, 0, 0, 0, 0};
        switch (index) {
            case 0:
                radius[0] = b;
                radius[1] = b;
                radius[6] = b;
                radius[7] = b;
                break;
            case 1:
                break;
            case 2:
                radius[2] = b;
                radius[3] = b;
                radius[4] = b;
                radius[5] = b;

                break;
        }
        drawable.setCornerRadii(radius);
        if (select) {
            drawable.setColor(0xffe2bb0a);
        } else {
            drawable.setColor(0xffffffff);
        }
        return drawable;
    }

    private MaxMinNumber getMaxMinNumber(List<Double> num) {
        MaxMinNumber number = new MaxMinNumber();
        double max;
        double min;
        if (num.size() > 2) {
            max = num.get(0);
            min = num.get(0);
            for (int i = 0; i < num.size(); i++) {
                if (max < num.get(i)) {
                    max = num.get(i);
                }
                if (min > num.get(i)) {
                    min = num.get(i);
                }

            }
        } else if (num.size() == 1) {
            max = num.get(0);
            min = num.get(0);
        } else {
            max = 0;
            min = 0;
        }

        number.setMaxNumbrt(max);
        number.setMinNumber(min);
        return number;
    }

    @Override
    public void onClick(int index) {
        if (bean != null && bean.getWhatday() != null && index-1 > -1 && index-1 < times.size())
            tv_time.setText(times.get(index-1));
    }
}
